/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Bdd.ControllerBDD;
import Main.Toolbox;
import Model.Saison;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "ControllerSaison", urlPatterns = { "/Saison/*" })
@RolesAllowed("admin")
public class ControllerSaison extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

        String pathInfo = request.getPathInfo();
        
        if (pathInfo.equals("/add")){
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/season-add.jsp");
            rd.forward(request, response);
        }
        else if (pathInfo.equals("/addForm")){
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/season-add.jsp");
            // Récupération des données du formulaire
            String annee = request.getParameter("annee");
            String dateDebut = request.getParameter("dateDebut");

            // Vérification des données et sauvegarde en BDD
            if (annee != null && dateDebut != null) {
                if(!annee.equals("") && !dateDebut.equals("")){
                    Saison s = new Saison();
                    s.setDateDebut(Toolbox.stringToDate(dateDebut));
                    s.setAnnee(Toolbox.stringToYear(annee));
                    List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
                    ls.add(s);
                    ControllerBDD.getINSTANCE().add(s);
                    request.setAttribute("message", "<div id=\"success\"><b>Opération réussie.</b><br />La saison a été ajoutée avec succès.</div>");
                }
                else
                {
                    request.setAttribute("message", "<div id=\"failure\"><b>Une erreur est survenue.</b><br />Un ou plusieurs champs sont manquants !</div>");
                }
            }
            rd.forward(request, response);
        }
        else if (pathInfo.equals("/delete")){
            String id = request.getParameter("id");
            if(id != null){
                
            }
        }
        else
        {
            List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
            request.setAttribute("seasonsList", ls);
            
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/season-list.jsp");
            rd.forward(request, response);
        }
    }

    public Saison getSaisonFromId(int id)
    {
        List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");

        for (Saison s : ls) {
            if (s.getIdSaison() == id){
                return s;
            }
        }
        return null;
    }
    
    public Saison getSaisonFromDate(Date date)
    {
        /*
        List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        int transacYear = gc.get(GregorianCalendar.YEAR);
        
        
        for (Saison s : ls) {
            GregorianCalendar gcs = new GregorianCalendar();
            int seasonYear = gcs.get(GregorianCalendar.YEAR);
            if (seasonYear == transacYear || seasonYear+1 == transacYear) 
            {
                return s;
            }
        }
        */
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
